<?php
namespace app\blogadmin\controller;

use think\Db;
use think\Request;
use app\blogadmin\model\Category as categoryM;

class Article extends Base{

    public function lst(){
        $res=Db::table('article')->alias('a')->join('category c','a.cateid=c.id')->field('a.*,c.cate_name')->paginate(7);
        $page=$res->render();
        $this->assign('articles',$res);
        $this->assign('page',$page);
        return $this->fetch();
    }


    public function add(){
        //获取所有的栏目
        $cateM=new categoryM();
        $cateLst=$cateM->cateTree();
        if(Request::instance()->isPost()){
            $data=input('post.');
            $data['tm']=time();
            //判断是否存在文件上传
            $uploadN=new Upload();
            //获取上传文件的信息
            $upload_info=$uploadN->UploadImg('file');
            //将数据放入 data 中
            if($upload_info['status']==1){
                $data['pic']=$upload_info['pathUrl'];
            }else{
                $this->error($upload_info['msg']);
//                return ['status'=>0,'msg'=>$upload_info['msg']];
            }
            //存入数据
            $res=Db::table('article')->insert($data);
            if($res){
                $this->success('添加文章成功','lst');
//                return ['status'=>1,'msg'=>'添加文章成功'];
            }else{
                $this->error('添加文章失败');
//                return ['status'=>0,'msg'=>'添加文章失败'];
            }
        }
        $this->assign('cateLst',$cateLst);
        return $this->fetch();
    }

    public function edit($id){
        //获取所有的栏目
        $cateM=new categoryM();
        $cateLst=$cateM->cateTree();
        //获取当前文章信息
        $cur_article=Db::table('article')->where(['id'=>intval($id)])->find();

        //获取提交过来的需要修改的数据
        if(Request::instance()->isPost()){
            $data=input('post.');
            $data['tm']=time();
            //判断是否存在文件上传
            $uploadN=new Upload();
            //获取上传文件的信息
            $upload_info=$uploadN->UploadImg('file');
            if($upload_info['status']==1){
                //上传文件成功
                $data['pic']=$upload_info['pathUrl'];
            }elseif ($upload_info['status']==2){
                //未上传文件的时候
                $data['pic']=$cur_article['pic'];
            }else{
                //上传文件出错
                $this->error($upload_info['msg']);
            }
            //存入数据
            $res=Db::table('article')->update($data);
            if($res){
                $this->success('修改文章成功','lst');
            }else{
                $this->error('修改文章失败');
            }
        }
        $this->assign('cur_article',$cur_article);
        $this->assign('cateLst',$cateLst);
        return $this->fetch();
    }

    /**
     * 删除文章
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del($id){
        $res=Db::table('article')->delete(intval($id));
        if($res) {
            $this->success('删除文章成功',"lst",'1');
        }else{
            $this->error('删除文章失败');
        }

    }

}